package com.playball.lottery.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Title: 整形动态数组 Description: 实现了整形数字的动态添加 Copyright: Copyright (c) 2003
 */
public class DynArrayInt {
    /**
     * 原始数组
     */
    private int[] data_All;

    /**
     * 计数器（数组长度）
     */
    private int size_count;

    /**
     * 构造器,初始长度默认为10
     */
    public DynArrayInt() {
	this(10);
    }

    /**
     * 构造器,设置数组的初始长度
     * 
     * @param iniSize
     *            int 数组的初始长度
     */
    public DynArrayInt(int iniSize) {
	data_All = new int[iniSize];
    }

    /**
     * 添加数据，调用checkAdd(int i)
     * 
     * @param i
     *            int 一个整形数字
     */
    public void addInt(int i) {
	// 判断是否增长
	this.checkAdd(size_count + 1);
	// 赋值
	data_All[size_count++] = i;
	// 添加时数组长度加一
    }

    public void addInt(int[] starData) {
	for (int i = 0; i < starData.length; i++) {
	    this.addInt(starData[i]);
	}
    }

    /**
     * 添加数字，判断是否增长
     * 
     * @param i
     *            int 一个整形数字
     */
    private void checkAdd(int i) {
	// 获得原来的大小
	int star = data_All.length;
	// 判断是否增长
	if (i > star) {
	    int starData[] = data_All;
	    // 排序
	    Arrays.sort(data_All);
	    // 设定增长大小
	    int endall = star * 2;
	    data_All = new int[endall];
	    System.arraycopy(starData, 0, data_All, 0, size_count);
	}
    }

    /**
     * 获取数据
     * 
     * @param i
     *            int 索引号
     * @return int
     */
    public int getInt(int i) {

	if (i < 0 || i >= size_count) {
	    throw new IndexOutOfBoundsException("超出最大或最小索引值，无法取得数据");
	} else {
	    return data_All[i];
	}
    }

    /**
     * 获取数据转换成字符串模式
     * 
     * @param i
     *            int 索引号
     * @return String
     */
    public String getIntToString(int i) {

	if (i < 0 || i >= size_count) {
	    throw new IndexOutOfBoundsException("超出最大或最小索引值，无法取得数据");
	} else {
	    String temp = data_All[i] + "";
	    if (temp.length() == 1) {
		temp = "0" + temp;
	    }
	    return String.valueOf(temp);
	}
    }

    /**
     * 获取数据转换成字符串模式
     * 
     * @param i
     *            int 索引号
     * @return String
     */
    public String getIntToStringForSingle(int i) {

	if (i < 0 || i >= size_count) {
	    throw new IndexOutOfBoundsException("超出最大或最小索引值，无法取得数据");
	} else {
	    String temp = data_All[i] + "";
	    if (temp.length() == 1) {
//		temp = "0" + temp;
	    }
	    return String.valueOf(temp);
	}
    }
    
    
    // Int转String，','分割
    public String getAllIntToStringForSingle(int[] tempint) {
	String starData = "";
	for (int i = 0; i < tempint.length; i++) {
	    String temp = tempint[i] + "";
	    if (temp.length() == 1) {
//		temp = "0" + temp;
	    }
	    starData = starData + temp + ",";
	}
	return starData;
    }
    
    public String getAllIntToStringForSingleKuai3(int[] tempint) {
	String starData = "";
	for (int i = 0; i < tempint.length; i++) {
		if(tempint[i]==3){//和值
			tempint[i]=111;
		}
		if(tempint[i]==18){
			tempint[i]=666;
		}
	    String temp = tempint[i] + "";
	    starData = starData + temp + ",";
	}
	return starData;
    }
    
    // Int转String，','分割
    public String getAllIntToString(int[] tempint) {
	String starData = "";
	for (int i = 0; i < tempint.length; i++) {
	    String temp = tempint[i] + "";
	    if (temp.length() == 1) {
		temp = "0" + temp;
	    }
	    starData = starData + temp + ",";
	}
	return starData;
    }

    /**
     * 删除数据
     * 
     * @param j
     *            int 一个要删除的整数
     */
    public void remove(int j) {
	for (int i = 0; i < size_count; i++) {
	    if (data_All[i] == j) {
		System.arraycopy(data_All, i + 1, data_All, i, size_count - i - 1); // 复制数据
		--size_count;
		return;
	    }
	}
    }

    /**
     * 删除数据
     * 
     * @param j
     *            int 一个要删除的索引
     */
    public void removeIndex(int j) {
	if (j < 0 || j >= size_count) {
	    throw new IndexOutOfBoundsException("超出最大或最小索引值，无法删除数据");
	} else {
	    System.arraycopy(data_All, j + 1, data_All, j, size_count - j - 1); // 复制数据
	    --size_count;
	    return;
	}
    }

    /**
     * 获取大小
     * 
     * @return int 获得数组长度
     */
    public int getSize() {
	return size_count;
    }

    /**
     * 获取数组对象
     * 
     * @return int[] 获得数组对象
     */
    public int[] getAllInt() {
	int[] starData = new int[size_count];
	System.arraycopy(data_All, 0, starData, 0, size_count);
	return starData;
    }

    /**
     * 获得数组对象,String格式
     * 
     * @return String[] 获得数组的对象
     */
    public String[] getAllIntToString() {
	int[] tempint = getAllInt();
	String[] starData = new String[tempint.length];
	for (int i = 0; i < starData.length; i++) {
	    starData[i] = String.valueOf(tempint[i]);
	}
	return starData;
    }

    /**
     * 删除全部内容
     */
    public void removeAll() {
	data_All = new int[10];
	size_count = 0;
    }

    public static void main(String[] args) {
	DynArrayInt diArrayInt = new DynArrayInt();
	for (int i = 0; i < 10; i++) {
	    diArrayInt.addInt(i);
	}
	diArrayInt.addInt(10);
	for (int i = 0; i < diArrayInt.getAllInt().length; i++) {
//	    System.out.println(diArrayInt.getAllInt()[i]);
	}
    }
    
    /**
     * 对选择的数字进行从小到大排序
     * @param ball  String ball ="3,9,8,7,6";
     * @return   sortedBall= "3,6,7,8,9";
     */
	public static String sortBall(String ball){
		String sortedBall="";
		if(!ball.equals("")){
			String[] lotNum = ball.split("\\,");
			List<Integer> list = new ArrayList<Integer>();
			for (int i = 0; i <lotNum.length; i++) {
//			System.out.println("lotNum[i]= "+lotNum[i]);
			list.add(Integer.parseInt(lotNum[i]));
			}
			Collections.sort(list);
//			System.out.println("list= "+list.toString());
			for (int i = 0; i <list.size(); i++) {
				sortedBall=sortedBall+","+list.get(i);
			}
			if(sortedBall.startsWith(",")){
				sortedBall=sortedBall.substring(1,sortedBall.length());
			}
//			System.out.println("sortedBall= "+sortedBall.toString());
		}
		return sortedBall;
	}
	
    /**
     * 对选择的数字进行从小到大排序,单个数位为2位
     * @param ball  String ball ="3,9,8,7,6";
     * @return   sortedBall= "03,06,07,08,09";
     */
	public static String sortBallDouble(String ball){
//		Log.v("sortBall", "ball= "+ball);
		String sortedBall="";
		if(!ball.equals("")){
			String[] lotNum = ball.split("\\,");
			List<Integer> list = new ArrayList<Integer>();
			for (int i = 0; i <lotNum.length; i++) {
//			System.out.println("lotNum[i]= "+lotNum[i]);
			list.add(Integer.parseInt(lotNum[i]));
			}
			Collections.sort(list);
//			System.out.println("list= "+list.toString());
			for (int i = 0; i <list.size(); i++) {
				String temp=String.valueOf(list.get(i));
				if (temp.length() == 1) {
					temp = "0" + temp;
				}
				sortedBall=sortedBall+","+temp;
			}
			if(sortedBall.startsWith(",")){
				sortedBall=sortedBall.substring(1,sortedBall.length());
			}
//			System.out.println("sortedBall= "+sortedBall.toString());
		}
		return sortedBall;
	}
}